/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.customexception.CustomException;
import com.yuhanginfo.modules.sys.entity.Orders;
import com.yuhanginfo.modules.sys.entity.Store;
import com.yuhanginfo.modules.sys.entity.User;
import com.yuhanginfo.modules.sys.service.OrdersChangeService;
import com.yuhanginfo.modules.sys.service.StoreService;
import com.yuhanginfo.modules.sys.utils.UserUtils;

/**
 * 订单变更Controller
 * @author yinhao
 * @version 2017-10-12
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/orderschange")
public class OrdersChangeController extends BaseController {

	private static final  Logger logger = LoggerFactory.getLogger(OrdersChangeController.class);
	
	@Autowired
	private OrdersChangeService ordersChangeService;
	
	@Autowired
	private StoreService storeService;
	
	@ModelAttribute
	public Orders get(@RequestParam(required=false) String id) {
		Orders entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = ordersChangeService.get(id);
		}
		if (entity == null){
			entity = new Orders();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:orderschange:view")
	@RequestMapping(value = {"list", ""})
	public String list(Orders orders, HttpServletRequest request, HttpServletResponse response, Model model) {
		User us = UserUtils.getUser();
		orders.setSysUserId(us.getId());//此处查询的不是业务员id,而是管理员id
		Page<Orders> page = ordersChangeService.findPage(new Page<Orders>(request, response), orders); 
		List<Store> storeList=storeService.findStoreBySysUser(us);
		model.addAttribute("store", storeList);
		model.addAttribute("page", page);
		return "modules/sys/order/orderchange/orderchangeList";
	}
	
	@RequiresPermissions("sys:orderschange:edit")
	@RequestMapping(value = "form")
	@ResponseBody
	public Map<String,Object> form(Orders orders) {
		Map<String,Object> resultMap = new HashMap<>();
		try{
			resultMap.putAll(ordersChangeService.findDetailList(orders));
			resultMap.put("resultCode", 1);
		}catch(CustomException ex){
			resultMap.put("resultCode", 0);
			resultMap.put("resultMessage", "订单状态已变更,请刷新页面");
			logger.info("form",ex);
		}
		return resultMap;
	}

	
	@RequiresPermissions("sys:orderschange:edit")
	@RequestMapping(value = "save")
	public String save(Orders orders, Model model, HttpServletRequest request, HttpServletResponse response) {
		String ordersId = request.getParameter("ordersId");
		String remarks = request.getParameter("remarks");
		String[] id = request.getParameterValues("id");
		String[] oldPrice = request.getParameterValues("oldPrice");
		String[] newPrice = request.getParameterValues("newPrice");
		String[] oldNum = request.getParameterValues("oldNum");
		String[] newNum = request.getParameterValues("newNum");
		try {
			User user = UserUtils.getUser();
			ordersChangeService.save(user,ordersId,remarks,id,oldPrice,newPrice,oldNum,newNum);
		} catch (Exception ex) {
			logger.info("save",ex);
		}
		return this.list(orders, request, response, model);
	}

}